 //
//  WSCustomPickerView.m
//  LANGirl
//
//  Created by 杨泓晔 on 16/3/12.
//  Copyright © 2016年 杨泓晔. All rights reserved.
//

#import "WSCustomPickerView.h"
#import "HZAreaPickerView.h"
#import "AppDelegate.h"

@interface WSCustomPickerView ()<HZAreaPickerDelegate> {

    NSArray *books;
}

@property (strong, nonatomic) UIPickerView *picker;
@property (nonatomic, strong) NSDictionary *dic;
@property (strong, nonatomic) HZAreaPickerView *locatePicker;
@property (strong, nonatomic) NSString *areaValue, *cityValue, *otherValue;
@property (nonatomic, strong) UIView *shareBackGroundView;
@property (nonatomic, strong) UIView *pickerView;
@property (nonatomic, strong) UIButton *cancel, *certain;
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, assign) NSInteger type;
@property (nonatomic, strong) UILabel *titleLabel;



@end

@implementation WSCustomPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"plist"];
        self.areaValue = @"北京 北京 朝阳";
        self.dic = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
        [self createBackground];
        [self createSubView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame dataArr:(NSArray *)dataArr {

    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.dataArr = dataArr;
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"plist"];
        self.areaValue = dataArr[0];
        self.dic = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
        [self createBackground];
        [self createSubView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame type:(NSInteger)type
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _type = type;
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"plist"];
        NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"YYYY-MM-dd"];
        self.areaValue = [formatter stringFromDate:[NSDate date]];
        self.dic = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
        [self createBackground];
        [self createSubView];
    }
    return self;
}

- (void)createSubView {
    
    self.pickerView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 240 * AUTO_WIDTH)];
    self.pickerView.backgroundColor = [UIColor whiteColor];
    [self.shareBackGroundView addSubview:_pickerView];
    
    [self cancelLocatePicker];
    if (_dataArr) {
        self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithOtherData delegate:self data:_dataArr];
    }
    else {
        if (_type == 1) {
            self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithDate delegate:self];
        }
        else {
            self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithStateAndCityAndDistrict delegate:self];
        }
    }
    [self.locatePicker showInView:_pickerView];
    
    UIView *lineView = [[UIView alloc] init];
    [self.pickerView addSubview:lineView];
    lineView.backgroundColor = TextLightGrayColor;
    lineView.alpha = 0.6;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(@1);
        make.right.left.mas_equalTo(@0);
        make.bottom.mas_equalTo(self.locatePicker.mas_top);
    }];
    
    [self createTitleLabel];
    [self createCancel];
    [self createCertain];
}

- (void)createBackground {
    
    UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    background.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    background.opaque = NO;
    UIGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
    [background addGestureRecognizer:gesture];
    self.shareBackGroundView = background;
    
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.window addSubview:self.shareBackGroundView];
}

- (void)createCancel {

    self.cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.pickerView addSubview:_cancel];
    [self.cancel setBackgroundImage:[[UIImage imageNamed:@"cha"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    _cancel.tintColor = GH;
    [self.cancel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.titleLabel);
        make.left.mas_equalTo(@8);
        make.size.mas_equalTo(CGSizeMake(40/2 *AUTO_WIDTH, 40/2 *AUTO_HEIGHT));
    }];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.pickerView addSubview:btn];
    btn.tag = 1000;
    [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(buttonDownAction:) forControlEvents:UIControlEventTouchDown];
    [btn addTarget:self action:@selector(buttonCancelAction:) forControlEvents:UIControlEventTouchUpOutside];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.mas_equalTo(@8);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
}

- (void)createTitleLabel {

    self.titleLabel = [[UILabel alloc] init];
    [self.pickerView addSubview:_titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(@0);
        make.bottom.equalTo(self.locatePicker.mas_top);
        make.centerX.mas_equalTo(@0);
    }];
    self.titleLabel.font = [UIFont systemFontOfSize:TEXTNAMEFONT];
    self.titleLabel.textColor = ZI;
}

- (void)createCertain {
    
    self.certain = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.pickerView addSubview:_certain];
    [self.certain setBackgroundImage:[[UIImage imageNamed:@"dui"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    _certain.tintColor = GH;
    [self.certain mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.titleLabel);
        make.right.mas_equalTo(@-8);
        make.size.mas_equalTo(CGSizeMake(40/2 *AUTO_WIDTH,40/2 *AUTO_HEIGHT));
    }];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.pickerView addSubview:btn];
    btn.tag = 1001;
    [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(buttonDownAction:) forControlEvents:UIControlEventTouchDown];
    [btn addTarget:self action:@selector(buttonCancelAction:) forControlEvents:UIControlEventTouchUpOutside];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(@8);
        make.right.mas_equalTo(@-8);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
}

- (void)buttonAction:(UIButton *)btn {

    if (btn.tag == 1001) {
        self.certain.tintColor = [UIColor blackColor];
        [self.delegate sendAddressInfo:self.areaValue];
    }
    else {
        self.cancel.tintColor = [UIColor blackColor];
    }
        
    [self removePicker];
}

- (void)buttonDownAction:(UIButton *)btn {

    if (btn.tag == 1001) {
        self.certain.tintColor = [UIColor colorWithRed:0.67 green:0.65 blue:0.51 alpha:1.00];
    }
    else {
        self.cancel.tintColor = [UIColor colorWithRed:0.67 green:0.65 blue:0.51 alpha:1.00];
    }
    
}

- (void)buttonCancelAction:(UIButton *)btn {

    if (btn.tag == 1001) {
        self.certain.tintColor = [UIColor blackColor];
    }
    else {
        self.cancel.tintColor = [UIColor blackColor];
    }
}

- (void)showPicker {
    
    self.hidden = NO;
    self.shareBackGroundView.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        
        self.shareBackGroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        self.pickerView.frame = CGRectMake(0, SCREEN_HEIGHT - 240 * AUTO_WIDTH, SCREEN_WIDTH, 240 * AUTO_WIDTH);
    }];
}

- (void)backgroundTapped:(UIGestureRecognizer *)tap {

    [self removePicker];
}

- (void)removePicker {

    [self removeFromSuperview];
    [self.shareBackGroundView removeFromSuperview];
}

#pragma mark - HZAreaPicker delegate
-(void)pickerDidChaneStatus:(HZAreaPickerView *)picker
{
    if (picker.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        self.areaValue = [NSString stringWithFormat:@"%@ %@ %@", picker.locate.state, picker.locate.city, picker.locate.district];
    } else if (picker.pickerStyle == HZAreaPickerWithDate){
        self.areaValue = [NSString stringWithFormat:@"%@-%@-%@", picker.locate.state, picker.locate.city, picker.locate.district];
    } else {
        self.areaValue = [NSString stringWithFormat:@"%@", picker.locate.state]; //picker.locate.state  学历
    }
}

-(void)cancelLocatePicker
{
    [self.locatePicker cancelPicker];
    self.locatePicker.delegate = nil;
    self.locatePicker = nil;
}

- (void)setTitleStr:(NSString *)titleStr {

    _titleStr  = titleStr;
    self.titleLabel.text = titleStr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
